package com.mt.roll.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mt.roll.core.api.Assert;
import com.mt.roll.core.base.BaseSupport;
import com.mt.roll.core.base.IErrorCode;
import com.mt.roll.core.dal.entity.EquipmentBom;
import com.mt.roll.core.dal.service.EquipmentBomServiceBiz;
import com.mt.roll.core.errorcode.ApiErrorCode;
import com.mt.roll.core.params.IdParam;
import com.mt.roll.core.utils.CodeGeneratorHelper;
import com.mt.roll.core.vo.R;
import com.mt.roll.wms.mapper.EquipmentBomEQMapper;
import com.mt.roll.wms.params.EquipmentBomParam;
import com.mt.roll.wms.params.EquipmentBomQueryParam;
import com.mt.roll.wms.service.EquipmentBomService;
import com.mt.roll.wms.vo.EquipmentBomVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Desc: ""
 * @Author: caixiang
 * @DATE: 2021/1/4 10:12
 */
@Service
@Transactional
public class EquipmentBomServiceImpl extends BaseSupport implements EquipmentBomService {
    @Autowired
    EquipmentBomEQMapper equipmentBomEQMapper;

    @Autowired
    EquipmentBomServiceBiz equipmentBomServiceBiz;

    @Override
    public R<IPage<EquipmentBomVo>> findAllByEQNameOrEQCode(EquipmentBomQueryParam equipmentBomBaseParam) {

        IPage<EquipmentBomVo> iPage = equipmentBomEQMapper.findAllByEQNameOrEQCode(
                new Page<>(equipmentBomBaseParam.getCurrent(), equipmentBomBaseParam.getSize()),
                equipmentBomBaseParam);
        return successful(iPage);
    }


    @Override
    public R<Boolean> create(EquipmentBomParam equipmentBomParam) {
        //在新建 物料BOM的 时候 ，要校验下 物料BOM是否存在
        String bomName = equipmentBomParam.getName();
        Long equipmentId = equipmentBomParam.getEquipmentId();
        QueryWrapper<EquipmentBom> wrapper = new QueryWrapper<>();
        wrapper.eq(EquipmentBom.NAME, bomName).eq(EquipmentBom.EQUIPMENT_ID, equipmentId);
        List<EquipmentBom> list = equipmentBomServiceBiz.list(wrapper);
        if(list.size()>=1){
            return failed(IErrorCode.CODE_FAILED,"此设备已存在该物料，请勿重复添加");
        }

        EquipmentBom equipmentBom = new EquipmentBom();
        Integer isEnable = 0;
        if(equipmentBomParam.getEnabled() !=null){
            isEnable = equipmentBomParam.getEnabled();
        }


        BeanUtils.copyProperties(equipmentBomParam,equipmentBom);
        if(equipmentBomParam.getCode() == null){
            equipmentBom.setCode(CodeGeneratorHelper.getEquipmentMaterialBomCode());
        }
        EquipmentBom equipmentBom1 = setCommonField(equipmentBom);
        equipmentBom1.setEnabled(isEnable);
        Boolean b = equipmentBomServiceBiz.save(equipmentBom1);
        if(b){
            return successful(b);
        }else {
            return failed(IErrorCode.CODE_FAILED,"创建失败");
        }
    }

    @Override
    public R<Boolean> update(EquipmentBomParam equipmentBomParam) {
        EquipmentBom equipmentBom = new EquipmentBom();
        BeanUtils.copyProperties(equipmentBomParam,equipmentBom);
        EquipmentBom equipmentBom1 = setCommonField(equipmentBom);
        if(equipmentBomParam.getEnabled() != null){
            equipmentBom1.setEnabled(equipmentBomParam.getEnabled());
        }
        Boolean b = equipmentBomServiceBiz.updateById(equipmentBom1);
        if(b){
            return successful(b);
        }else {
            return failed(IErrorCode.CODE_FAILED,"更新失败");
        }
    }

    @Override
    public R<EquipmentBomVo> findById(IdParam idParam) {
        Assert.notNull(ApiErrorCode.INVALID_PARAMETER, idParam.getId());
        EquipmentBom byId = equipmentBomServiceBiz.getById(idParam.getId());
        if(byId!=null){
            EquipmentBomVo equipmentBomVo = EquipmentBomVo.builder().build();
            BeanUtils.copyProperties(byId,equipmentBomVo);
            return successful(equipmentBomVo);
        }
        return failed(IErrorCode.CODE_FAILED,"查找的内容不存在");
    }

    @Override
    public R<Boolean> delete(IdParam idParam) {
        Assert.notNull(ApiErrorCode.INVALID_PARAMETER, idParam.getId());
        boolean b = equipmentBomServiceBiz.removeById(idParam.getId());
        if(b){
            return successful(b);
        }else {
            return failed(IErrorCode.CODE_FAILED,"删除失败");
        }
    }
}
